package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.constant.BompAppParamConstant;
import cc.rengu.igas.bomp.common.constant.DubboMethodConstant;
import cc.rengu.igas.bomp.common.dao.BizTmpMapper;
import cc.rengu.igas.bomp.common.dao.MchntBaseInfoMapper;
import cc.rengu.igas.bomp.common.dao.WechatMchntInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.BizTmpMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.MchntBaseInfoMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.WechatMchntInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.BizTmp;
import cc.rengu.igas.bomp.common.entity.MchntBaseInfo;
import cc.rengu.igas.bomp.common.entity.WechatMchntInfo;
import cc.rengu.igas.bomp.common.enums.MchntStatusEnum;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.common.util.InitParamUtil;
import cc.rengu.igas.bomp.core.model.WechatIntegration;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.request.WechatRealNameApplyCancelRequest;
import cc.rengu.igas.bomp.facade.response.WechatRealNameApplyCancelResponse;
import cc.rengu.igas.channel.wechat.facade.base.Header;
import cc.rengu.igas.channel.wechat.facade.request.WechatCancelApplymentRequest;
import cc.rengu.igas.channel.wechat.facade.response.WechatCancelApplymentResponse;
import cc.rengu.igas.channel.wechat.facade.result.Result;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import com.alibaba.fastjson.JSONObject;

/**
 * @author bank
 * @version 1.0
 * @date 2019/08/15
 * @Description: 微信商户实名认证申请撤销
 */
public class WechatRealNameApplyCancelService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        WechatRealNameApplyCancelRequest realNameApplyCancelRequest = new WechatRealNameApplyCancelRequest();
        ConvertUtil.convertOutput(realNameApplyCancelRequest);
        return realNameApplyCancelRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {

        WechatRealNameApplyCancelRequest realNameApplyCancelRequest = (WechatRealNameApplyCancelRequest) request;
        BizResponse<WechatRealNameApplyCancelResponse> bizResponse = new BizResponse<>();
        WechatRealNameApplyCancelResponse realNameApplyCancelResponse = new WechatRealNameApplyCancelResponse();
        bizResponse.setResult(realNameApplyCancelResponse);
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        realNameApplyCancelResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        realNameApplyCancelResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        String instId = realNameApplyCancelRequest.getHeader().getInstId();
        String mchntNo = realNameApplyCancelRequest.getMchntNo();

        MchntBaseInfoMapper mchntBaseInfoMapper = new MchntBaseInfoMapperImpl();
        MchntBaseInfo mchntBaseInfo = mchntBaseInfoMapper.selectMchntBaseInfoByPrimaryKey(instId, mchntNo);
        if (null == mchntBaseInfo) {
            rglog.error("微信商户实名认证申请撤销失败, 根据商户号查询商户基础信息为空，instId:{},mchntNo:{}",
                    instId, mchntNo);
            throw new BizException(RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespCode(), RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespDesc());
        }
        if (!MchntStatusEnum.NORMAL.getStatus().equals(mchntBaseInfo.getMchntStatus())) {
            rglog.error("商户状态不合法,status:<{}>", mchntBaseInfo.getMchntStatus());
            throw new BizException(RespCodeEnum.MCHNT_STATUS_ABNORMAL.getRespCode(), RespCodeEnum.MCHNT_STATUS_ABNORMAL.getRespDesc());
        }

        WechatMchntInfoMapper wechatMchntInfoMapper = new WechatMchntInfoMapperImpl();
        WechatMchntInfo wechatMchntInfo = wechatMchntInfoMapper.selectWechatMchntInfoByPrimaryKey(instId, mchntNo);
        if (null == wechatMchntInfo || StringUtil.isEmptyOrNull(wechatMchntInfo.getWechatMchntNo())) {
            rglog.error("未微信进件.");
            throw new BizException(RespCodeEnum.WECHAT_REAL_NAME_APPLY_ERROR.getRespCode(), "未微信进件,请先进行第三方进件");
        }

        if (AppParamConstant.NO.equals(wechatMchntInfo.getRealNameFlag())) {
            rglog.error("未实名认证申请.");
            throw new BizException(RespCodeEnum.WECHAT_REAL_NAME_APPLY_ERROR.getRespCode(), "未实名认证申请");
        }

        if (StringUtil.isEmptyOrNull(wechatMchntInfo.getRealNameApplyNo())) {
            rglog.error("实名认证申请单号为空.");
            throw new BizException(RespCodeEnum.WECHAT_REAL_NAME_APPLY_ERROR.getRespCode(), "实名认证申请单号为空");
        }

        WechatCancelApplymentRequest cancelApplymentRequest = new WechatCancelApplymentRequest();
        Header header = InitParamUtil.initWechatHeader("cancelapply", instId, sysSeqNum);
        cancelApplymentRequest.setChannelId(ChannelEnum.WXZF.getChannelType());
        cancelApplymentRequest.setHeader(header);
        cancelApplymentRequest.setChannelType(wechatMchntInfo.getDstChannelType());
        cancelApplymentRequest.setBusinessCode(wechatMchntInfo.getRealNameBizNo());
        // cancelApplymentRequest.setApplymentId(wechatMchntInfo.getRealNameApplyNo());
        DubboService dubboService = new DubboServiceImpl();
        Result result = (Result) dubboService.callDubboService(DubboMethodConstant.DUBBO_WECHAT_MCHNT_SERVICE, "cancelApplyment", cancelApplymentRequest);
        if (null == result || !result.isSuccess()) {
            rglog.error("调用微信撤销实名验证申请失败.");
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        WechatCancelApplymentResponse wechatCancelApplymentResponse = (WechatCancelApplymentResponse) result.getResult();
        if (!DubboMethodConstant.DUBBO_SUCCESS.equals(wechatCancelApplymentResponse.getRespCode())) {
            rglog.error("调用微信撤销实名验证申请失败!,errorCode:{},errorMsg:{}",
                    wechatCancelApplymentResponse.getRespCode(), wechatCancelApplymentResponse.getRespDesc());
            throw new BizException(wechatCancelApplymentResponse.getRespCode(), wechatCancelApplymentResponse.getRespDesc());
        }

        wechatMchntInfo.setRealNameFlag(AppParamConstant.NO);
        wechatMchntInfo.setLastOprId(realNameApplyCancelRequest.getUserId());
        wechatMchntInfo.setLastUpdateTime(DateUtil.getCurrentDateTime(BompAppParamConstant.yyyy_MM_dd_HHmmssSSS));

        BizTmpMapper bizTmpMapper = new BizTmpMapperImpl();
        BizTmp wechatMchntInfoBizTmp = bizTmpMapper.selectBizTmpByRealId(wechatMchntInfo.getInstId(), wechatMchntInfo.getId());
        WechatMchntInfo bizTmpWechatMchntInfo = JSONObject.parseObject(InitParamUtil.getJsonString(wechatMchntInfoBizTmp), WechatMchntInfo.class);
        bizTmpWechatMchntInfo.setRealNameFlag(wechatMchntInfo.getRealNameFlag());
        bizTmpWechatMchntInfo.setLastOprId(wechatMchntInfo.getLastOprId());
        bizTmpWechatMchntInfo.setLastUpdateTime(wechatMchntInfo.getLastUpdateTime());
        InitParamUtil.initBizTempData(wechatMchntInfoBizTmp, JSONObject.toJSONString(bizTmpWechatMchntInfo));

        WechatIntegration wechatIntegration = new WechatIntegration();
        wechatIntegration.updateWechatMchntInfoAndBizTmp(wechatMchntInfo, wechatMchntInfoBizTmp);
        bizResponse.setResult(realNameApplyCancelResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        WechatRealNameApplyCancelResponse wechatRealNameApplyCancelResponse = (WechatRealNameApplyCancelResponse) bizResponse.getResult();
        ConvertUtil.convertInput(wechatRealNameApplyCancelResponse);
    }
}
